package org.hgvc.webapp.ets.jsf.beans.admin;

import java.util.List;

import javax.faces.component.UIParameter;
import javax.faces.event.ActionEvent;

import org.hgvc.webapp.ets.bo.Agreement;
import org.hgvc.webapp.ets.bo.InvoiceDtl;
import org.hgvc.webapp.ets.bo.Member;
import org.hgvc.webapp.ets.bo.MemberComp;
import org.hgvc.webapp.ets.bo.MemberInd;
import org.hgvc.webapp.ets.bo.MemberTransaction;
import org.hgvc.webapp.ets.constant.CommonConstant;
import org.hgvc.webapp.ets.constant.TerminateConstant;
import org.hgvc.webapp.ets.jsf.beans.AckBean;
import org.hgvc.webapp.ets.spring.service.MemberService;
import org.jocsb.base.AbstractBean;
import org.jocsb.base.exception.BaseException;
import org.jocsb.base.util.FacesUtils;
import org.jocsb.base.util.SpringUtils;



public class AdminMemberAgreementMgmtBean extends AbstractBean {

	public final static String BEAN_NAME="adminMemberAgreementMgmtBean";
	public final static String NAV_LIST ="adminMemberAgreementMgmt";
	private MemberService memberService;
	private Member member;
	private List<Agreement> agreementList;
	private Agreement agreement;
	private boolean userAccountSuspended;
	

	public String changeMembershipAction(){
		
		return AdminChangeMembershipBean.NAV_LIST.concat("_"+getRoleName());
	}
	
	public String navIndSelectAction(){
		AdminSelectMembershipBean b = (AdminSelectMembershipBean)FacesUtils.getManagedBean(AdminSelectMembershipBean.BEAN_NAME );
		b.setMember(member);
		b.setNewAgreement(true);
		b.setAction( AdminCreateIndProspectBean.NAV_CREATION);
		return AdminSelectMembershipBean.NAV_SELECT_ACTION.concat("_"+getRoleName());
	}
	
	public void changeMembershipActionListener(ActionEvent ae) throws Exception{
		UIParameter param = (UIParameter)ae.getComponent().findComponent("agreementId2");
		String agreementId =(String)param.getValue();
		AdminChangeMembershipBean bean = (AdminChangeMembershipBean)FacesUtils.getManagedBean(AdminChangeMembershipBean.BEAN_NAME);
		Agreement agreement = (Agreement)commonDao.getObject(Agreement.class, agreementId); 
		bean.iniBean(agreement.getMember().getMemberId(),(Agreement)commonDao.getObject(Agreement.class, agreementId));
		
	}
	
	public String rescheduleAction(){
		return AdminAgreementRescheduleBean.BEAN_NAME.concat("_"+getRoleName());
	}
	
	public void rescheduleActionListener(ActionEvent ae) throws Exception{
		UIParameter param = (UIParameter)ae.getComponent().findComponent("agreementId3");
		String agreementId =(String)param.getValue();
		AdminAgreementRescheduleBean bean = (AdminAgreementRescheduleBean)FacesUtils.getManagedBean(AdminAgreementRescheduleBean.BEAN_NAME);
		 bean.iniBean( (Agreement)commonDao.getObject(Agreement.class, agreementId));
		
	}
	
	public String reActivateUserAccountAction(){
		AckBean ackBean = (AckBean) getNewManagedBean(AckBean.BEAN_NAME);
		ackBean.setAckHeaderTitle(SpringUtils.getMessage("common.acknowledgement", new Object[]{}));
		ackBean.setAckMsg(SpringUtils.getMessage("user.account.activated", new Object[]{member.getLoginUser().getUsername()}));
		ackBean.setNextActionOutcome(NAV_LIST.concat("_"+getRoleName()));
		this.userAccountSuspended=false;
		return "etsAck".concat("_"+getRoleName());
	}
	public void reActivateUserAccountListener(ActionEvent event){
		try{
			this.memberService.reActivateUserAccount(member.getLoginUser());
			this.member=this.memberService.getMemberByUserId(member.getLoginUser().getUserId());
			if (member.getMemberType().equalsIgnoreCase(CommonConstant.MEMBER_TYPE_IND)){
				MemberInd memberInd = this.memberService.getMemberIndByMemberId(member.getMemberId());
				member.setName(memberInd.getNameAsIcPassport());
			}else{
				MemberComp memberComp = this.memberService.getMemberCompByMemberId(member.getMemberId());
				member.setName(memberComp.getCompName());
			}
		}catch(BaseException be){
			initErrorBean(be);
		}
	}
	public boolean isUserAccountSuspended() {
		userAccountSuspended=false;
		if (member.getLoginUser().getStatus().equalsIgnoreCase(TerminateConstant.SUSPENDED))
			userAccountSuspended=true;
		
		return userAccountSuspended;
	}
	
	public Member getMember() {
		return member;
	}

	public void setMember(Member member) {
		this.member = member;
	}

	public MemberService getMemberService() {
		return memberService;
	}

	public void setMemberService(MemberService memberService) {
		this.memberService = memberService;
	}

	public String backToList(){
		return NAV_LIST.concat("_"+getRoleName());
	}
	
	public String navMemberAgreementMgmt(Member member){
		this.member = member;
		return NAV_LIST.concat("_"+getRoleName());
	}

	public List<Agreement> getAgreementList() {
		try{
			agreementList = memberService.getAgreementListByMemberId(member.getMemberId());
		}catch(BaseException ex){
			ex.printStackTrace();
			initErrorBean(ex);
		}
		return agreementList;
	}
	
	public void navViewPointActionListener(ActionEvent actionEvent){
		UIParameter param = (UIParameter)actionEvent.getComponent().findComponent("agreementId");
		String id = (String)param.getValue();
		for(Agreement agreement:agreementList){
			if(agreement.getAgreementId().equalsIgnoreCase(id)){
				this.agreement = agreement;
				break;
			}
		}
	}
	
	public String navViewPointAction(){
		AdminMemberAgreementPointEnquiryBean bean = (AdminMemberAgreementPointEnquiryBean)FacesUtils.getManagedBean(AdminMemberAgreementPointEnquiryBean.BEAN_NAME);
	    try{
	    	List<MemberTransaction> memberTransactionList = this.memberService.getMemberTransactionList(agreement.getAgreementId());	
	    	bean.setMemberTransactionList(memberTransactionList);
	    	
	    	List<InvoiceDtl> invoiceList = memberService.getInvoiceDtlByAgreementId(agreement.getAgreementId());
	    	bean.setHistoryInvoiceList(invoiceList);
	    	
	    	/*if (member.getMemberType().equalsIgnoreCase(CommonConstant.MEMBER_TYPE_IND)){
				MemberInd memberInd = this.memberService.getMemberIndByMemberId(member.getMemberId());
				member.setName(memberInd.getNameAsIcPassport());
			}else{
				MemberComp memberComp = this.memberService.getMemberCompByMemberId(member.getMemberId());
				member.setName(memberComp.getCompName());
			}*/
	    	bean.setMember(member);
	    }catch(BaseException be){
	    	initErrorBean(be);
	    }
		
		return bean.navListAction(agreement);
	}
	
	public void navViewAgreementInvoiceActionListener(ActionEvent actionEvent){
		UIParameter param = (UIParameter)actionEvent.getComponent().findComponent("vagreementId");
		String id = (String)param.getValue();
		for(Agreement agreement:agreementList){
			if(agreement.getAgreementId().equalsIgnoreCase(id)){
				this.agreement = agreement;
				break;
			}
		}
	}
	
	public String navViewAgreementInvoiceAction(){
		AdminMemberAgreementInvoiceBean bean = (AdminMemberAgreementInvoiceBean)FacesUtils.getManagedBean(AdminMemberAgreementInvoiceBean.BEAN_NAME);
		return bean.navListAction(agreement);
	}
	
}
